<?php

namespace App\Http\Controllers\Admin;

use App\Models\Card;
use App\Models\Allowance;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AllowanceController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $status = Allowance::$status;
        return view('admin.allowance.index', compact('status'));
    }

    public function data(Request $request)
    {
        $fields = [
            'v_shipping.shipping_no',
            'v_allowance.*'
        ];
        $model = Allowance::select($fields)
            ->leftJoin('v_shipping', 'v_shipping.id', '=', 'v_allowance.shipping_id');
        if ($request->get('status')) {
            $model = $model->where('v_allowance.status', $request->get('status'));
        }
        if ($request->get('card_number')) {
            $model = $model->where('v_allowance.card_number', 'like', '%' . $request->get('card_number') . '%');
        }
        $res = $model->orderBy('v_allowance.id', 'desc')
            ->paginate($request->get('limit', 30))
            ->toArray();

        return $this->table($res['data'], $res['total']);
    }
}
